﻿using API_Service.DBHelper;
using Dapper;
using System;
using System.Data;
using System.Linq;
using System.Reflection;

namespace API_Service.BizLogic.WIPControl
{
    public class StationRecService: BaseLogicService
    {
        public ExecutionResult<dynamic> QueryStationRecInfo(dynamic vQConditoin)
        {
            ExecutionResult<dynamic> result;
            result = new ExecutionResult<dynamic>();
            var dbParams = new DynamicParameters();
            #region SQL and parameters
            string _columns = " work_date,line_name,group_name ";
            string _groupBy = " GROUP BY work_date,line_name,group_name ";
            string _where = " WHERE 1=1 AND work_date = @work_date ";
            dbParams.Add("@work_date", (string)vQConditoin.work_date, DbType.String, ParameterDirection.Input);
            if (!string.IsNullOrEmpty(vQConditoin.mo))
            {
                _columns = _columns + ",mo_number ";
                _where = _where + " AND mo_number=@mo_number ";
                _groupBy = _groupBy + ",mo_number ";
                dbParams.Add("@mo_number", (string)vQConditoin.mo, DbType.String, ParameterDirection.Input);
            }
            if (!string.IsNullOrEmpty(vQConditoin.model))
            {
                _columns = _columns + ",model_name ";
                _where = _where + " AND model_name=@model_name ";
                _groupBy = _groupBy + ",model_name ";
                dbParams.Add("@model_name", (string)vQConditoin.model, DbType.String, ParameterDirection.Input);
            }
            if (!string.IsNullOrEmpty(vQConditoin.line))
            {
                _where = _where + " AND line_name=@line_name ";
                dbParams.Add("@line_name", (string)vQConditoin.line, DbType.String, ParameterDirection.Input);
            }
            if (!string.IsNullOrEmpty(vQConditoin.group))
            {
                _where = _where + " AND group_name=@group_name ";
                dbParams.Add("@group_name", (string)vQConditoin.group, DbType.String, ParameterDirection.Input);
            }
            string sql = "SELECT " + _columns + " ,SUM (a.wip_qty) wip_qty,SUM (a.pass_qty) pass_qty, " +
                                   " SUM (a.fail_qty) fail_qty, SUM (a.repass_qty) repass_qty,SUM (a.refail_qty) refail_qty " +
                               " FROM  dbo.r_station_rec_t AS a " + _where + _groupBy;
            #endregion

            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    if (conn.State != ConnectionState.Open)
                        conn.Open();

                    result.Datas = conn.Query(sql, dbParams).ToArray();
                    result.Status = true;
                    result.Message = "查询产能信息成功..";
                }
                catch (Exception ex)
                {
                    result.Status = false;
                    result.Message = "查询产能信息异常，异常信息：" + ex.Message;
                    Logger.WriteError(MethodBase.GetCurrentMethod().Name, ex);
                }
            }

            return result;
        }
    }
}